package com.sn.springboot_netty.controller;

import com.sn.springboot_netty.model.User;
import com.sn.springboot_netty.server.IUserServer;
import com.sn.springboot_netty.utils.TemplateWordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "用户管理",description = "管理用户相关接口")
@RestController
public class UserController extends BaseController{

    @Autowired
    private IUserServer userServer;

    @GetMapping("/users")
    @ApiOperation(value = "获取所有用户列表",notes = "用户列表")
    public Object queryUserList(){
        return userServer.queryUserList();
    }

    @GetMapping("/user")
    @ApiOperation(value = "获取用户信息 by id",notes = "用户信息 by id")
    @Cacheable(value = "user",key = "'user_'+#p0",cacheManager = "redisCacheManager")
    public Object queryUser(String id){
        return userServer.queryUserById(1);
    }

    @GetMapping("/exportUsers")
    public void exportUsersWord(HttpServletRequest request, HttpServletResponse response){
        String fileTile = "用户列表";
        String ftlPath = "test.ftl";
        List<User> users = userServer.queryUserList();
        Map<String,Object> map = new HashMap<>();
        map.put("userList",users);
        TemplateWordUtil.exportTemplateWord(request,response,map,fileTile,ftlPath);
    }

    @GetMapping("/exportUsers2")
    public void exportUsersWord2(HttpServletRequest request, HttpServletResponse response){
        String fileTile = "用户列表";
        String ftlPath = "test2.ftl";
        String imagePath = TemplateWordUtil.class.getResource("/img").getPath()+"/2.jpg";
        List<User> users = userServer.queryUserList();
        for (int i = 0;i < 10;i++){
            users.addAll(users);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("userList",users);
        map.put("img",TemplateWordUtil.getImageBase64(imagePath));
        TemplateWordUtil.exportTemplateWord(request,response,map,fileTile,ftlPath);
    }

}
